All files / model document.ts

92.31% Statements 36/39
85.71% Branches 12/14
85.71% Functions 12/14
91.67% Lines 33/36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116                                  2x     2x               2x       7721x 7721x 7721x     7721x     2x 96213x     2x 2x 2x     2x 1x     2x 962x                 2x 86x           2x 5850x     2x 46202x 46202x 46202x 46202x         2x             2x 1074x   2x       2x 89x             2x     2x              
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { SnapshotVersion } from '../core/snapshot_version';
import { fail } from '../util/assert';
import { AnyJs } from '../util/misc';
 
import { DocumentKey } from './document_key';
import { FieldValue, JsonObject, ObjectValue } from './field_value';
import { FieldPath } from './path';
 
export interface DocumentOptions {
  hasLocalMutations: boolean;
}
 
export class Document {
  readonly hasLocalMutations: boolean;
 
  constructor(
    readonly key: DocumentKey,
    readonly version: SnapshotVersion,
    readonly data: ObjectValue,
    options: DocumentOptions
  ) {
    this.hasLocalMutations = options.hasLocalMutations;
  }
 
  field(path: FieldPath): FieldValue | undefined {
    return this.data.field(path);
  }
 
  fieldValue(path: FieldPath): AnyJs {
    const field = this.field(path);
    return field ? field.value() : undefined;
  }
 
  value(): JsonObject<AnyJs> {
    return this.data.value();
  }
 
  isEqual(other: Document | null | undefined): boolean {
    return (
      other instanceof Document &&
      this.key.isEqual(other.key) &&
      this.version.isEqual(other.version) &&
      this.data.isEqual(other.data) &&
      this.hasLocalMutations === other.hasLocalMutations
    );
  }
 
  toString(): string {
    return (
      `Document(${this.key}, ${this.version}, ${this.data.toString()}, ` +
      `{hasLocalMutations: ${this.hasLocalMutations}})`
    );
  }
 
  static compareByKey(d1: MaybeDocument, d2: MaybeDocument): number {
    return DocumentKey.comparator(d1.key, d2.key);
  }
 
  static compareByField(field: FieldPath, d1: Document, d2: Document): number {
    const v1 = d1.field(field);
    const v2 = d2.field(field);
    Eif (v1 !== undefined && v2 !== undefined) {
      return v1.compareTo(v2);
    } else {
      return fail("Trying to compare documents on fields that don't exist");
    }
  }
}
 
/**
 * A class representing a deleted document.
 * Version is set to 0 if we don't point to any specific time, otherwise it
 * denotes time we know it didn't exist at.
 */
export class NoDocument {
  constructor(readonly key: DocumentKey, readonly version: SnapshotVersion) {}
 
  toString(): string {
    return `NoDocument(${this.key}, ${this.version})`;
  }
 
  isEqual(other: NoDocument): boolean {
    return (
      other &&
      other.version.isEqual(this.version) &&
      other.key.isEqual(this.key)
    );
  }
 
  static compareByKey(d1: MaybeDocument, d2: MaybeDocument): number {
    return DocumentKey.comparator(d1.key, d2.key);
  }
}
 
/**
 * A union type representing either a full document or a deleted document.
 * The NoDocument is used when it doesn't exist on the server.
 */
export type MaybeDocument = Document | NoDocument;